<template>
  <a-modal
    :title="title"
    :width="800"
    :visible="visible"
    :confirmLoading="confirmLoading"
    @ok="handleOk"
    @cancel="handleCancel"
    cancelText="关闭"
  >
    <a-spin :spinning="confirmLoading">
      <a-form :form="form">
        <a-form-item :labelCol="labelCol" :wrapperCol="wrapperCol" label="标签">
          <a-input placeholder="请输入标签" v-decorator="['name', validatorRules.name]" />
        </a-form-item>
        <a-form-item :labelCol="labelCol" :wrapperCol="wrapperCol" label="类型">
          <a-select placeholder="全部类型" v-decorator="['type', validatorRules.type]">
            <a-select-option :value="item.val" v-for="(item, index) in typeUl" :key="index">{{ item.name }}</a-select-option>
          </a-select>
        </a-form-item>
      </a-form>
    </a-spin>
  </a-modal>
</template>

<script>
import ApiTree from '@/api/common/tagList';
import ImageUpload from '@/components/ImageUpload'

import { Slime } from '@/mixins/slime';
export default {
  name: 'TagListModal',
  mixins: [Slime],
  components: {
    ImageUpload,
  },
  data() {
    return {
      title: '操作',
      id:0,
      model: {},
      form: this.$form.createForm(this),
      validatorRules: {
        name: { rules: [{ required: true, message: '请输入标签!' }] },
        type: { rules: [{ required: true, message: '请选择类型!' }] },
      },
      typeUl:[
        {name:'商家标签', val:1},
        {name:'用户标签', val:2},
      ],
    }
  },
  created() {},
  methods: {
    // 增
    crudCreate() {
      this.crudUpdate({})
    },
    // 改
    crudUpdate(record) {
      this.form.resetFields()  //重新设置领域
      this.model = Object.assign({}, record)
      this.visible = true;
      this.$nextTick(() => {
        this.form.setFieldsValue({
          name: record.name,
          type: record.type,
        });
        this.id = record.id
      });
    },
    // 提交
    handleOk() {
      const that = this;
      this.form.validateFields((err, values) => {
        if (!err) {
          that.confirmLoading = true;
          const param = {
            ...values, 
            id: that.id, 
          };
          if (!that.id) {
            that.submitCreate(param);
          } else {
            that.submitUpdate(param);
          }
        }
      });
    },
    // 提交 增
    submitCreate(param) {
      const that = this;
      ApiTree.getAdd(param)
        .then((res) => {
          that.$message.success('新增成功');
        })
        .finally(() => {
          that.closeModal();
        });
    },
    // 提交 改
    submitUpdate(param) {
      const that = this;
      ApiTree.getUpdate(param)
        .then((res) => {
          that.$message.success('编辑成功');
        })
        .finally(() => {
          that.closeModal();
        });
    },
    handleCancel(){
      this.closeModal();
    },
    // 弹窗-关闭
    closeModal() {
      this.confirmLoading = false;
      this.visible = false;
      this.form.resetFields();
      this.$emit('closeModal');
    },
  }, 
}
</script>

<style lang="less" scoped>
</style>